<template>
  <el-dialog @close="cancel" :visible="showRoleDialog" title="角色分配">
    <el-checkbox-group v-model="ids" style="margin-bottom: 20px">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>
    <el-row type="flex" justify="center">
      <el-button @click="save" size="small" type="primary">确认</el-button>
      <el-button @click="cancel" size="small">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
// 角色列表
import { getRoleList } from '@/api/settings'
// 当前用户信息
import { getUserInfoAvatar } from '@/api/user'
import { setRoles } from '@/api/employees'
export default {
  name: 'role',
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false,
      required: true
    },
    userId: {
      type: String,
      required: true,
      default: ''
    }
  },
  data () {
    return {
      // 根据跟人id获取角色分配
      ids: [],
      //   角色列表
      roleList: []
    }
  },
  created () {
    //   角色列表
    this.getRoleList()
    // this.getUserInfo()
  },
  methods: {
    cancel () {
      this.$emit('update:showRoleDialog', false)
    },
    // 角色列表
    async getRoleList () {
      let res = await getRoleList({
        page: 1,
        pagesize: 99999
      })
      //   console.log(res)
      this.roleList = res.data.rows
    },
    // 获取当前用户信息（包含角色分配）
    async getUserInfo (userId) {
      let res = await getUserInfoAvatar(userId)
      //   console.log(res)
      this.ids = res.data.roleIds
    },
    // 设置员工的角色
    async save () {
      let res = await setRoles({
        id: this.userId,
        roleIds: this.ids
      })
      console.log(res)
      this.$message.success('成功分配角色！')
      //   隐藏对话框
      this.cancel()
    }
  }
}
</script>

<style></style>
